#!/usr/bin/env python
# -*- coding: UTF-8  -*-
import os
import sys
import string
#import xlrd
import xml.dom.minidom
#import Pythons.Scripts
import codecs #用于写txt文档时编码UTF-8

from random import randint
from pyExcelerator import *
from xml.dom.minidom import Node

update_file="/tmp/update_info.txt"
result=""
result1=""
def svn_update_info(arg1,arg2,arg3):
    if os.path.exists(arg1):
        doc = xml.dom.minidom.parse(arg1) 
        root = doc.documentElement
    else:
        print "文件不存在！！"
        sys.exit(1)
    for node in root.getElementsByTagName("project"):
      if node.nodeType == node.ELEMENT_NODE:
        if node.getAttribute("name") == arg2:
          for package in node.childNodes:
            if package.nodeType == node.ELEMENT_NODE:
              result=package.getAttribute(arg3)
              print "%s" % result

def svn_update_info1(arg1,arg2,arg3,arg4):
    if os.path.exists(arg1):
        doc = xml.dom.minidom.parse(arg1) 
        root = doc.documentElement
    else:
        print "文件不存在！！"
        sys.exit(1)
    #print "arg1 %s" %arg1
    #print "arg2 %s" %arg2
    #print "arg3 %s" %arg3
    #print "arg4 %s" %arg4
    for node in root.getElementsByTagName("project"):
      if node.nodeType == node.ELEMENT_NODE:
        if node.getAttribute("name") == arg2:
          for package in node.childNodes:
            if package.nodeType == node.ELEMENT_NODE:
              package_dir=package.getAttribute("package_dir")
              package_name=package.getAttribute("package_name") 
 	      version=package.getAttribute("version") 
              strings="%s/%s %s %s" %(arg3,package_dir,package_name,version)
              result = codecs.open(arg4,'a',"utf-8");
              result.write(strings);
              result.write("\n");
              result.close();



if __name__ == '__main__':
    if len(sys.argv) < 4:
        print "Usage: %s /path/to/repo/url [project] keyword" % sys.argv[0]
        sys.exit(1)
    args = sys.argv
    if len(sys.argv) >= 4:
       svn_update_info(args[1],args[2],args[3])

